package com.lyk.coursearrange.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lyk.coursearrange.dao.JobAsLessonscheduleDao;
import com.lyk.coursearrange.dao.JobDao;
import com.lyk.coursearrange.dao.LessonscheduleDao;
import com.lyk.coursearrange.entity.Job;
import com.lyk.coursearrange.entity.JobAsLessonschedule;
import com.lyk.coursearrange.entity.Lessonschedule;
import com.lyk.coursearrange.entity.Stages;
import com.lyk.coursearrange.entity.dto.JobDto;
import com.lyk.coursearrange.service.JobService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class JobServiceImpl extends ServiceImpl<JobDao, Job> implements JobService {


    @Autowired
    private JobDao jobDao;
    @Autowired
    private LessonscheduleDao lessonscheduleDao;
    @Autowired
    private JobAsLessonscheduleDao jobAsLessonscheduleDao;

    @Override
    public List<Job> joblist(Map<String, String> param) {
        Long page = null;
        Long limit = null;
        Long offset =null;
        String jobName =null;
        if(param.get("page")!=null&&!param.get("page").equals("")){
            page = Long.valueOf(param.get("page"));
        }
        if(param.get("limit")!=null&&!param.get("limit").equals("")){
            limit = Long.valueOf(param.get("limit"));
        }
        if(param.get("jobName")!=null&&!param.get("jobName").equals("")){
            jobName = param.get("jobName");
        }
        if (page!=null&&limit!=null){
            offset =   (page - 1) * limit;
        }


        return jobDao.joblist(jobName,offset,limit);
    }

    @Override
    public Long jobcount(Map<String, String> param) {
        String jobName =null;
        if(param.get("jobName")!=null&&!param.get("jobName").equals("")){
            jobName = param.get("jobName");
        }
        return jobDao.jobcount(jobName);
    }

    @Override
    public JobDto jobdetails(Map<String, String> param) {
        Long job_id = null;
        if (param.get("jobId")!=null&&!param.get("jobId").equals("")){
            job_id = Long.valueOf(param.get("jobId"));
        }
        return jobDao.jobdetails(job_id);
    }

    @Override
    public boolean jobset(JobDto j) {
        UpdateWrapper<Job> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("job_basepay",j.getJobBasepay());
        updateWrapper.set("job_name",j.getJobName());
        //先删数据
        jobDao.deletebyjobasle(j.getJobId());
        //再删关系
        jobDao.deletelebyjobid(j.getJobId());
        List<Lessonschedule> lessonschedules = j.getLessonschedules();
        lessonschedules.forEach(le -> {
            int a = lessonscheduleDao.insert(le);
            if (a>0){
                JobAsLessonschedule jobAsLessonschedule = JobAsLessonschedule.builder()
                        .jobId(j.getJobId())
                        .leId(le.getLeId())
                        .build();
                jobAsLessonscheduleDao.insert(jobAsLessonschedule);
                System.out.println("添加成功");
            }else {
                System.out.println("添加失败");
            }
        });
        updateWrapper.eq("job_id",j.getJobId());
        return update(updateWrapper);
    }

    @Override
    public Long selectjobname(String job_name) {
        return jobDao.selectjobname(job_name);
    }
}
